package com.neusoft.neuiotms.dcom.converter.malfunction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.neuiotms.dcom.dto.malfunction.MalfunctionSecondLineDTO;
import com.neusoft.neuiotms.dcom.model.malfunction.MalfunctionSecondLine;
import com.neusoft.neuiotms.dcom.repository.malfunction.MalfunctionSecondLineRepository;
import com.neusoft.neuiotms.micro.dcam.DcamMicroService;

@Component
public class MalfunctionSecondLineConverter extends AbstractConverter<MalfunctionSecondLine, MalfunctionSecondLineDTO> {

    @Autowired
    private MalfunctionSecondLineRepository malfunctionSecondLineRepository;
    @Autowired
    private DcamMicroService assetMicroService;

    @Override
    public MalfunctionSecondLine toModel(final MalfunctionSecondLineDTO dto) {
        final MalfunctionSecondLine model;
        if (dto.isNew()) {
            model = new MalfunctionSecondLine();
        } else {
            model = this.malfunctionSecondLineRepository.getOne(dto.getId());
        }
        // 资产编码
        if (!dto.getAssetObjs().isEmpty()) {
            final String assets =
                    dto.getAssetObjs().stream().map(e -> e.containsKey("code") ? e.get("code").toString() : null)
                            .filter(k -> null != k).collect(Collectors.joining(","));
            model.setAssets(assets);
        } else {
            model.setAssets("");
        }
        model.setContext(dto.getContext());
        if (!CollectionUtils.isEmpty(dto.getEngineerCode())) {
            final String engineerCode = dto.getEngineerCode().stream().collect(Collectors.joining(","));
            model.setEngineerCode(engineerCode);
        }
        if (!CollectionUtils.isEmpty(dto.getEngineerName())) {
            final String engineerName = dto.getEngineerName().stream().collect(Collectors.joining(","));
            model.setEngineerName(engineerName);
        }
        model.setFaultLevel(dto.getFaultLevel());
        model.setOperatorCode(dto.getOperatorCode());
        model.setOperatorName(dto.getOperatorName());
        model.setTaskId(dto.getTaskId());
        model.setWorkNum(dto.getWorkNum());
        model.setTransfer(dto.isTransfer());
        model.setTransferEngineerCode(dto.getTransferEngineerCode());
        model.setTransferEngineerName(dto.getTransferEngineerName());
        model.setTransferReason(dto.getTransferReason());
        model.setTransferUserName(dto.getTransferUserName());

        return model;
    }

    @Override
    public MalfunctionSecondLineDTO toDTO(final MalfunctionSecondLine model, final boolean forListView) {

        final MalfunctionSecondLineDTO dto = new MalfunctionSecondLineDTO();
        dto.setId(model.getId());
        dto.setAssets(model.getAssets());
        dto.setContext(model.getContext());

        if (StringUtils.isNotEmpty(model.getEngineerCode())) {
            final List<String> codes = Stream.of(model.getEngineerCode().split(",")).collect(Collectors.toList());
            dto.setEngineerCode(codes);
        }

        if (StringUtils.isNotEmpty(model.getEngineerName())) {
            final List<String> names = Stream.of(model.getEngineerName().split(",")).collect(Collectors.toList());
            dto.setEngineerName(names);
        }

        dto.setFaultLevel(model.getFaultLevel());
        dto.setOperatorCode(model.getOperatorCode());
        dto.setOperatorName(model.getOperatorName());
        dto.setTaskId(model.getTaskId());
        dto.setWorkNum(model.getWorkNum());
        dto.setTransfer(model.getTransfer());
        dto.setTransferEngineerCode(model.getTransferEngineerCode());
        dto.setTransferEngineerName(model.getTransferEngineerName());
        dto.setTransferReason(model.getTransferReason());
        dto.setTransferUserName(model.getTransferUserName());

        if (!StringUtils.isEmpty(model.getAssets())) {
            final List<String> codes = Stream.of(model.getAssets().split(",")).collect(Collectors.toList());
            if (null != codes) {
                final Map<String, String[]> assetCodes = new HashMap<>();
                assetCodes.put("assetCodes", codes.toArray(new String[codes.size()]));
                final ResultListDTO<Map<String, Object>> assets =
                        this.assetMicroService.AssetByCodes(assetCodes);
                if (assets.isSuccess()) {
                    dto.setAssetObjs(assets.getData());
                }
            }
        }

        return dto;
    }

}
